#!/usr/bin/env python

import threading
import time
import random
import multiprocessing
import urllib,urllib2
import httplib

q=multiprocessing.Queue()
mutex=threading.Lock()

def do_request(ip):
	proxy_handler=urllib2.ProxyHandler({'http':'http://'+ip})
	opener=urllib2.build_opener(proxy_handler)
	data=urllib.urlencode({"id":"549"})
	#data=urllib.urlencode({"id":"552"})
	req=urllib2.Request("http://sunshine.hust.edu.cn/zt/jksj2009/save.asp?action=htc")
	req2=urllib2.Request("http://acm.hust.edu.cn/thx/")
	req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.11 (KHTML, like Gecko) Chrome/2.0.181.1 Safari/530.11')
	req2.add_header('User-Agent','Google ChiRouMa?')
	try:
		opener.open(req2,timeout=3)
		f=opener.open(req,data,timeout=60)
	except:
		return 0
	return len(f.read())

class VoteThread(threading.Thread):
	def __init__(self,threadName):
		threading.Thread.__init__(self,name=threadName)
	
	def run(self):
		global q
		cnt=0
		mutex.acquire()
		while not q.empty():
			ip=q.get()
			cnt=cnt+1
			mutex.release()
			retlen=do_request(ip)
			mutex.acquire()
			if retlen>0:
				print "[%s][%d]"%(self.name,cnt),ip,retlen
		mutex.release()

def main():
	thr=[]
	f=open("newip","r")
	for x in f:
		q.put(x.strip())
	for i in xrange(100):
		th=VoteThread("%02d"%i)
		thr.append(th)
		th.start()
	for th in thr:
		th.join()

def main2():
	do_request("60.6.205.26:808")

if __name__=='__main__':
	for i in xrange(10):	
		main()

